<template>
  <div>
    <el-tabs v-model="caliActiveName" @tab-click="handleClick">
      <el-tab-pane label="候选人公示  " name="zero">
        <div style="margin-left: 40px">
          <el-date-picker
            type="datetime"
            placeholder="选择日期时间"
            default-time="12:00:00"
            v-model="openItem.openTime"
          >
          </el-date-picker>

          <el-button
            type="primary"
            size="mini"
            @click="insertOpen"
            :disabled="openStatu"
            style="margin-left: 30px"
            >公示</el-button
          >

          <el-button type="primary" size="mini" @click="reload">重置</el-button>
        </div>

        <div>
          <el-table :data="cddList" border style="margin-top: 20px">
            <!-- 添加序号列 -->
            <el-table-column
              label="序号"
              type="index"
              width="70"
              align="center"
              index-method="indexMethod"
            />

            <el-table-column
              label="供应商名称名称"
              align="center"
              prop="supName"
            />

            <el-table-column
              label="最终报价(万元)"
              align="center"
              prop="finalQuote"
            />
            <el-table-column
              label="最终得分"
              align="center"
              prop="finalScore"
            />
            <el-table-column label="是否推荐" align="center" prop="recommended">
              <template slot-scope="scope">
                <dict-tag
                  class="dict-box"
                  :options="dict.type.pur_recom_state"
                  :value="scope.row.recommended"
                />
              </template>
            </el-table-column>
            <el-table-column label="排名" align="center" prop="ranking" />
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane label="确定中标人" name="one">
        <div>
          <el-button
            type="primary"
            size="mini"
            style="margin-left: 40px; margin-bottom: 20px"
            @click="updateState"
            :disabled="useState"
            >确定</el-button
          >
          <el-button size="mini" @click="reload" :disabled="useState"
            >重 置</el-button
          >
          <el-table
            :data="cddList"
            border
            style="width: 1300px; margin: 0 auto"
            @selection-change="handleSelectionChange"
            ref="multipleTable"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="排名" align="center" prop="ranking" />

            <el-table-column
              label="供应商名称名称"
              align="center"
              prop="supName"
            />

            <el-table-column
              label="最终报价(万元)"
              align="center"
              prop="finalQuote"
            />
            <el-table-column
              label="最终得分"
              align="center"
              prop="finalScore"
            />
            <el-table-column label="是否推荐" align="center" prop="recommended">
              <template slot-scope="scope">
                <dict-tag
                  class="dict-box"
                  :options="dict.type.pur_recom_state"
                  :value="scope.row.recommended"
                />
              </template>
            </el-table-column>
            <el-table-column label="是否中标" align="center" prop="winningBid">
              <template slot-scope="scope">
                <dict-tag
                  class="dict-box"
                  :options="dict.type.pur_eav_statu"
                  :value="scope.row.winningBid"
                />
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane label="中标结果" name="thiree">
        <el-button
          type="primary"
          size="mini"
          style="margin-left: 20px; margin-bottom: 20px"
          @click="openTime"
          >公示</el-button
        >
        <el-button type="primary" size="mini">发送中标通知书</el-button>
        <el-table
          :data="casCddList"
          border
          style="width: 1300px; margin: 0 auto"
          ref="multipleTable"
          @selection-change="handleSelectionDelvChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="排名" align="center" prop="ranking" />

          <el-table-column
            label="供应商名称名称"
            align="center"
            prop="supName"
          />

          <el-table-column
            label="最终报价(万元)"
            align="center"
            prop="finalQuote"
          />
          <el-table-column label="最终得分" align="center" prop="finalScore" />
          <el-table-column label="是否推荐" align="center" prop="recommended">
            <template slot-scope="scope">
              <dict-tag
                class="dict-box"
                :options="dict.type.pur_recom_state"
                :value="scope.row.recommended"
              />
            </template>
          </el-table-column>
          <el-table-column label="是否中标" align="center" prop="winningBid">
            <template slot-scope="scope">
              <dict-tag
                class="dict-box"
                :options="dict.type.pur_eav_statu"
                :value="scope.row.winningBid"
              />
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <!--中标公示-->
    <el-dialog
      title="中标结果公示"
      :visible.sync="openBindDialogVisible"
      width="40%"
      :before-close="handleCloseOpenBind"
    >
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            公示时间
          </template>
          <el-date-picker
            v-model="openBindTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-descriptions-item>
      </el-descriptions>

      <span slot="footer" class="dialog-footer">
        <el-button @click="openBindDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOpenBindState">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listCdd, getCdd, delCdd, addCdd, updateCdd } from "@/api/purs/cdd";
import { updateBind } from "@/api/purs/bind";
import {
  listOpen,
  getOpen,
  delOpen,
  addOpen,
  updateOpen,
} from "@/api/purs/open";
export default {
  dicts: ["pur_eav_statu", "pur_recom_state"],
  props: {
    openMessage: {
      type: Object,
    },
  },
  data() {
    return {
      caliActiveName: "zero",
      openBindDialogVisible: false,
      total: 0,
      cddList: [],
      openBindTime: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      querycasParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 用于存放选中的行数据
      selectedRows: [],
      updateCddItem: {},
      useState: false,
      casCddList: [],
      delvSelection: [],
      openStatu: false,
      updateDev: {},
      openItem: {},
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getcasList();
    this.checkIsOpen();
  },
  methods: {
    reload() {
      this.getList();
    },

    checkIsOpen() {
      listOpen({
        bindCode: this.openMessage.bindCode,
      }).then((res) => {
        if (res.rows.length > 0) {
          this.openStatu = true;
        }
      });
    },

    insertOpen() {
      if (this.openItem.openTime == null) {
        this.$modal.msgError("请选择公示时间");
      } else {
        this.openItem.bindCode = this.openMessage.bindCode;

        addOpen(this.openItem).then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("候选人公示成功");
            this.checkIsOpen();
          }
        });
      }
    },

    /** 查询候选人列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.bindCode = this.openMessage.bindCode;
      listCdd(this.queryParams).then((response) => {
        this.cddList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    reload() {
      this.getList();
      this.getcasList();
    },

    saveOpenBindState() {
      if (this.openBindTime) {
        this.updateDev.openTime = this.openBindTime;
        updateCdd(this.updateDev).then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("中标结果公示成功");
            this.openBindDialogVisible = false;
          }
        });
      } else {
        this.$modal.msgError("信息填充不完整");
      }
    },

    handleSelectionDelvChange(selection) {
      this.delvSelection = selection;
    },

    handleCloseOpenBind() {
      this.openBindDialogVisible = false;
    },
    openTime() {
      if (this.delvSelection.length == 0) {
        this.$modal.msgError("请选择要处理的数据");
      } else {
        if (this.delvSelection.length > 1) {
          this.$modal.msgError("只能依次选择一条数据");
        } else {
          this.updateDev = this.delvSelection[0];

          this.openBindDialogVisible = true;
        }
      }
    },
    getState() {},

    handleClick(tab, event) {
      console.log(tab, event);
    },
    getcasList() {
      this.querycasParams.winningBid = "2";
      this.querycasParams.bindCode = this.openMessage.bindCode;
      listCdd(this.querycasParams).then((response) => {
        this.casCddList = response.rows;
        this.total = response.total;
      });
    },
    getList() {
      this.loading = true;
      this.queryParams.bindCode = this.openMessage.bindCode;

      listCdd(this.queryParams).then((response) => {
        this.cddList = response.rows;
        this.total = response.total;
        this.loading = false;
        response.rows.forEach((item) => {
          if (item.winningBid == "2") {
            this.useState = true;
          }
        });
      });
    },
    updateState() {
      if (this.selectedRows.length == 0) {
        this.$modal.msgError("请选择中标源");
      } else {
        if (this.selectedRows.length > 1) {
          this.$modal.msgError("只能选择一条数据进行操作");
        } else {
          this.updateCddItem = this.selectedRows[0];
          this.updateCddItem.winningBid = 2;
          updateCdd(this.updateCddItem).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("设置成功");
              let bind = {
                bindCode: this.openMessage.bindCode,
                status: 6,
              };

              updateBind(bind).then();
              this.getcasList();
              this.getList();
            }
          });
          this.$refs.multipleTable.clearSelection();
        }
      }
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
  },
};
</script>

<style scope>
</style>